<!-- 扫码咨询 -->
<template>
  <publicPage>
    <view class="consultInfoPage">
      <view
        class="disFlxJS linh-50 px-15"
        style="border-bottom: solid 1px #f5f5f5"
      >
        <view class="fz-16">
          <text v-if="scene">{{infoObj.userName || ''}}邀您</text>
          <text>即时咨询（会诊）</text>
        </view>
        <view class="fz-16 fw-7">
          <text style="color: #bb9313">{{infoObj.cost || 0}}云豆</text>
          <text>/次</text>
        </view>
      </view>
      <InfoCard :item="infoObj" />
      <view style="border-top: solid 30rpx #f5f5f5">
        <u-cell size="large" :isLink="true">
          <view slot="title">
            <text>当前余额</text>
            <text class="px-15" style="color: #bb9313">{{userInfo.balance || 0}}</text>
            <text>云豆</text>
          </view>
          <!-- <view slot="value">
            <text class="pr-15" style="color: #5a86ff" @click.stop="goRecharge()">充值</text>
          </view> -->
        </u-cell>
        <u-cell size="large" :isLink="true" @click="changePatient">
          <view slot="title">咨询人</view>
          <view slot="value">
            <text class="pr-15">{{userInfo.userName}}</text>
            <text class="pr-15">{{userInfo.sex === '1' ? '女' : '男'}}</text>
            <!-- <text class="pr-15">28</text> -->
          </view>
        </u-cell>
      </view>
    </view>

    <view class="mx-16 py-30">
      <u-button
        type="primary"
        :text="scene? '确认付费' : '立即拨打'"
        :customStyle="{
          fontSize: '32rpx',
          borderRadius: '20rpx',
          height: '88rpx',
        }"
        :throttleTime="500"
        @click="callNow"
      ></u-button>   
      <view class="fz-12 color9 py-17 tc" v-if="scene">咨询说明：会诊由您的医生向会诊专家发起咨询</view>
    </view>
    <button class="buttonStyleNone fiexBtn" open-type="contact">
      <view class="tc colorZS fz-14">联系客服</view>
    </button>
    
    <NoLogin
      :show="loginShow"
      tips="点击立即登录,登录后即可立即咨询"
      @close="loginShow = false"
      @loginReturn="getscene"
    />

    <StatusPopup :show="czShow" type="error" btnText="去充值" @close="czShow = false" @confirm="goRecharge()">
      <view class="pt-20">您的余额不足，请充值</view>
    </StatusPopup>

    <OfficialAccount :show="accountShow" @close="_closeFn" />
  </publicPage>
</template>  

<script>
import orderApi from "@/api/order";
import InfoCard from "@/components/infoCard.vue";
import StatusPopup from "../components/statusPopup.vue";
import NoLogin from "../components/noLogin.vue";
import OfficialAccount from "@/components/officialAccount.vue";

// from_expert_id 永远都不是咨询人的ID - 陈建才
export default {
  components: {
    InfoCard,
    StatusPopup,
    NoLogin,
    OfficialAccount
  },
  computed: {
    // 用户信息
    userInfo() {
      return this.$store.state.userInfo;
    }
  },
  data() {
    return {
      loginShow: false,
      scene: "", // 此参数存在则为扫码进入
      getParams: {},
      infoObj: {},
      czShow: false,
      accountShow: false
    };
  },
  onLoad(options){
    const that = this
    if(options.scene){
      uni.setNavigationBarTitle({title: '扫码咨询'})
      const scene = decodeURIComponent(options.scene)
      console.log(scene,'扫码入参');
      that.scene = scene.split(',')
      

      // 扫码进入
      if(!that.$store.state.userInfo.token || !that.$store.state.userInfo.id){
        that.loginShow = true
        return
      }
      that.getscene()
    }else{
      that.getParams = options
      that.getList()
    }
  },

  methods: {
    // 获取专家详情
    async getList() {
      const { data } = await orderApi.getExpertInfo(this.getParams)
      if(this.scene && data.costMap){
        // 扫码咨询,取优惠价格
        data.cost = data.costMap.newcost
      }
      this.infoObj = data

      // 余额不足
      if(this.userInfo.balance < this.infoObj.cost){
        this.czShow = true
      }
    },
    getscene(){
      const that = this
      if(that.scene[0]){
        that.getParams.from_expert_id = that.scene[0]
        setTimeout(() => {
          that.$store.dispatch("sendCustomMessage",{
            type: 'scanCode',
            toID: that.getParams.from_expert_id,
            title: '扫码成功'
          });// 发送扫码成功
        }, 1000);
      }
      if(that.scene[1]){
        that.getParams.expertId = that.scene[1]
      }
      that.getList()
    },
    // 切换就诊人
    changePatient(){
      uni.showToast({
        title: '暂未开发家庭成员',
        icon: 'none'
      })
    },
    // 去充值
    goRecharge(){
      this.czShow = false
      this.goPage(`/pagesC/recharge/index?czPlice=${this.infoObj.cost - this.userInfo.balance}`)
    },
    // 立即呼叫
    callNow(){
      const that = this
      // 余额不足
      if(this.userInfo.balance < this.infoObj.cost){
        this.czShow = true
        return
      }
      if(that.scene){ // 扫码进入
        let params = {
          callType: 1, // 即时咨询 1 预约咨询 2
          to_expert_ids: this.getParams.expertId,
          from_expert_id: this.getParams.from_expert_id
        }
        orderApi.cellExp(params).then(res=>{
          this.$store.dispatch("sendCustomMessage",{
            type: 'consult',
            toID: this.getParams.from_expert_id,
            title: '咨询付费',
            meetingData: res.data
          });// 发送咨询付费
          uni.showToast({
            title: '付费成功',
            icon: 'none',
            complete: ()=>{ 
              setTimeout(() => {
                if(!that.userInfo.mpOpenid){ // 未关注公众号
                  that.accountShow = true
                }else{
                  that.$store.dispatch("getUserInfo");
                  that.goBack()
                }
              }, 1500);
            }
          })
        })
      }else{
        let params = {
          callType: 1, // 即时咨询 1 预约咨询 2
          to_expert_ids: this.getParams.expertId
        }
        orderApi.cellExp(params).then(res=>{
          this.$store.commit("SET_CELLINFO", {
            orderNo: res.data.orderNo,
            meetingRoomNumber: res.data.meetingRoomNumber,
            password: res.data.password,
            id: this.infoObj.id,
            userName: this.infoObj.userName,
            headimg: this.infoObj.headimg
          }); // 呼叫信息
          this.$store.dispatch("getUserInfo");
          this.goPage('/pagesB/meeting/callPanel?meetingStatus=1')
        })
      }
    },
    _closeFn(){
      this.accountShow = false
      this.$store.dispatch("getUserInfo");
      this.goBack()
    }
  },
};
</script>
<style lang='scss' scoped>
.consultInfoPage {
  background-color: #ffffff;
}

.fiexBtn{
  position: fixed;
  left: 0;
  width: 100%;
  line-height: 60rpx;
  bottom: 40rpx;
}
</style>